package com.stu.wgt.test.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.wgt.test.bean.ProductCategories;
import com.stu.wgt.test.mapper.ProductCategoriesMapper;
import com.stu.wgt.test.service.ProductCategoriesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wgt
 * @since 2024-02-04
 */
@Service
public class ProductCategoriesServiceImpl extends ServiceImpl<ProductCategoriesMapper, ProductCategories> implements ProductCategoriesService {

    @Autowired
    ProductCategoriesMapper productCategoriesMapper;

    @Override
    public int insert(ProductCategories productCategories) {
        QueryWrapper<ProductCategories> qw = new QueryWrapper<>();
        qw.eq("name", productCategories.getName());
        if (productCategories.getName().equals(productCategoriesMapper.selectList(qw))) {
            return 0;
        }
        return productCategoriesMapper.insert(productCategories);
    }

    @Override
    public int delete(int id) {
        return productCategoriesMapper.deleteById(id);
    }

    @Override
    public int update(ProductCategories productCategories) {
        QueryWrapper<ProductCategories> qw = new QueryWrapper();
        qw.eq("id", productCategories.getId());
        return productCategoriesMapper.update(productCategories, qw);
    }

    @Override
    public List<ProductCategories> selectProductCategories() {
        return null;
    }

    @Override
    public List<ProductCategories> selectById(int id) {
        QueryWrapper<ProductCategories> qw = new QueryWrapper<>();
        qw.eq("id", id);
        return productCategoriesMapper.selectList(qw);
    }

    @Override
    public List<ProductCategories> selectByParentId(int id) {
        QueryWrapper<ProductCategories> qw = new QueryWrapper<>();
        qw.eq("parent_id", id);
        return productCategoriesMapper.selectList(qw);
    }

    /**
     * 查询产品分类
     *
     * @param type
     * @param pageNum
     * @param pageSize
     * @return 树级结构
     */
    @Override
    public IPage<ProductCategories> searchProductCategories(Integer type, int pageNum, int pageSize) {
        Page<ProductCategories> page = new Page<>(pageNum, pageSize);
        List<ProductCategories> categories;
        int sum = 0;
        if (type == null) {
            // 获取所有分类
            categories = productCategoriesMapper.selectList(null);
        } else if (type == 1) {
            // 根据type获取对应级别的分类
            QueryWrapper<ProductCategories> qw = new QueryWrapper<>();
            qw.le("level", type);// level <= 1
            categories = productCategoriesMapper.selectList(qw);
            sum = categories.size();
        } else if (type == 2) {
            // 根据type获取对应级别的分类
            QueryWrapper<ProductCategories> qw = new QueryWrapper<>();
            qw.lt("level", type);  // level < 2
            categories = productCategoriesMapper.selectList(qw);
            // 根据type决定是否进行递归查询
            for (ProductCategories category : categories) {
                QueryWrapper<ProductCategories> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("parent_id", category.getId()).eq("level", type); // level = 2 and parent_id = 父ID
                List<ProductCategories> children = productCategoriesMapper.selectList(queryWrapper);
                category.setChildren(children);
            }
            sum = categories.size();
        } else {
            QueryWrapper<ProductCategories> qw = new QueryWrapper<>();
            qw.le("level", 1);
            categories = productCategoriesMapper.selectList(qw);
            for (ProductCategories category : categories) {
                category.setChildren(getChildrenCategories(category.getId(), 2));
            }
            sum = categories.size();
        }
        page.setTotal(sum);
        page.setRecords(categories);
        return page;
    }

    /**
     * @param parentId 子分类的父ID
     * @param level    子分类层级
     * @return
     */
    private List<ProductCategories> getChildrenCategories(Integer parentId, int level) {
        QueryWrapper<ProductCategories> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId).eq("level", level);
        List<ProductCategories> children = productCategoriesMapper.selectList(queryWrapper);
        // 递归查询子分类的子分类
        if (level < 2)
            for (ProductCategories child : children) {
                child.setChildren(getChildrenCategories(child.getId(), level + 1));
            }
        return children;
    }

    @Override
    public List<ProductCategories> findItemCatList(Integer level) {
        return null;
    }

    @Override
    public int selectByName(String name) {
        QueryWrapper<ProductCategories> qw = new QueryWrapper<>();
        qw.eq("name", name);
        List<ProductCategories> productCategories = productCategoriesMapper.selectList(qw);
        return productCategories.get(0).getId();
    }
}
